mod app;
use app::Cell;
use std::time::Duration;
use std::process::Command;
use std::thread;

fn main() {
    // x and grid axis, limited fornow
    let mut grid: Vec<Vec<Cell>> = Vec::new();
    for y in 0..50 {
        grid.push(Vec::new());
        for x in 0..120 {
            grid[y].push(Cell::new(false,0));
        }
    }
    grid = glider(grid.clone());
    for i in 0 .. 100000 {
        Command::new("clear");
        draw(grid.clone());
        grid = step(grid);
        grid = step2(grid);
    }
}

fn step( mut grid: Vec<Vec<Cell>>) -> Vec<Vec<Cell>>{
  //  let    grid2 = grid.clone();

    for y in 1 .. grid.len()-1 {
        for x in 1 .. grid[y].len()-1{
            grid=getadj(grid.clone(), y, x);
        }
    }
    grid
}
fn step2( mut grid: Vec<Vec<Cell>>) -> Vec<Vec<Cell>>{
    for y in 1 .. grid.len()-1 {
        for x in 1 .. grid[y].len()-1{
            match grid[y][x].adj{
                3 => grid[y][x].state =true,
                2 => grid[y][x].state = grid[y][x].state,
                _ => grid[y][x].state =false,
            }
        }
    }
    grid
}

fn getadj(mut grid:Vec<Vec<Cell>>,y:usize, x:usize) -> Vec<Vec<Cell>> {

    let mut adj:i32 = 0;
    for i in x-1..x+2{
        if grid[y+1][i].state == true {adj += 1;}
    }
    for i in x-1 .. x+2 {
        if grid[y-1][i].state == true {adj += 1;}
    }
    if grid[y][x-1].state == true {adj += 1;}
    if grid[y][x+1].state == true {adj += 1;}

    grid[y][x].adj = adj;
            

    grid
}

fn draw(grid:Vec<Vec<Cell>>) {
   // let output = Command::new("clear").output().unwrap_or_else(|e| { 
   //     panic!("failed to execute process: {}", e)
   // });
   // println!("{}", String::from_utf8_lossy(&output.stdout));
    let mut n = grid.len()-1;
    while n > 0 {
        let mut row = "".to_owned();
        for i in 0..grid[n].len() {
            if grid[n][i].state == true {
//                row.push_str(&grid[n][i].adj.to_string());
                row.push_str("0");
            }
            else {
                row.push_str(" ");;
            }
                
        }
 //       println!("{}", String::from_utf8_lossy(&output.stdout));
        println!("{}", row);
        n -= 1;
    }
    thread::sleep(Duration::from_millis(100));
    
}

fn glider(mut grid: Vec<Vec<Cell>>) -> Vec<Vec<Cell>> {
    grid[20][30].state=true;
    grid[19][31].state=true;
    grid[18][29].state=true;
    grid[18][30].state=true;
    grid[18][31].state=true;
    grid[10][10].state=true;
    grid[10][9].state=true;
    grid[9][10].state=true;
    grid[9][9].state=true;

    grid[9][15].state=true;
    grid[9][16].state=true;
    grid[9][17].state=true;
    grid[9][18].state=true;
    grid[9][19].state=true;

    
    grid[14][10].state=true;
    grid[15][10].state=true;
    grid[16][10].state=true;
    grid[17][10].state=true;
    grid[18][10].state=true;
    grid[14][11].state=true;
    grid[15][11].state=true;
    grid[16][11].state=true;
    grid[17][11].state=true;
    grid[18][11].state=true;
    grid[14][12].state=true;
    grid[15][12].state=true;
    grid[16][12].state=true;
    grid[17][12].state=true;
    grid[18][12].state=true;
    grid
}

